//
//  KYSettingController.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/9/21.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYSettingController.h"
#import "KYSettingItem.h"
#import "KYSettingArrowItem.h"
#import "KYSettingCheckItem.h"
#import "KYSettingCheckGroup.h"
#import "KYSettingItemCell.h"

@interface KYSettingController ()

@end

@implementation KYSettingController
- (NSMutableArray *)groups
{
    if (_groups == nil) {
        _groups = [NSMutableArray array];
    }
    return _groups;
}

- (KYSettingGroup *)addGroup
{
    KYSettingGroup *group = [KYSettingGroup group];
    [self.groups addObject:group];
    return group;
}

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (instancetype)init
{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.tableView.backgroundView = nil;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.sectionHeaderHeight = 0; // 每一组的头部高度
    self.tableView.sectionFooterHeight = 6; // 每一组的尾部高度
    
    // 底部控件
    UIView *footer = [[UIView alloc] init];
    footer.frame = CGRectMake(0, 0, 0, 1);
    self.tableView.tableFooterView = footer;
    
    // 设置上边距
    self.tableView.contentInset = UIEdgeInsetsMake(-25, 0, 0, 0);
    
    // 设置行高
    self.tableView.rowHeight = 60;
}

#pragma mark ------  Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.groups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    KYSettingGroup *group = self.groups[section];
    return group.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KYSettingItemCell *cell = [KYSettingItemCell cellWithTableView:tableView];
    cell.indexPath = indexPath;
    
    KYSettingGroup *group = self.groups[indexPath.section];
    cell.item = [group.items objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark - 代理
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    KYSettingGroup *group = self.groups[section];
    return group.footer;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    KYSettingGroup *group = self.groups[section];
    return group.header;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 0.取出模型
    KYSettingGroup *group = [self.groups objectAtIndex:indexPath.section];
    KYSettingItem *item = [group.items objectAtIndex:indexPath.row];
    
    // 1.0操作不需要参数的
    if (item.option) {
        item.option();
    }
    
    // 1.1传递参数的
    if (item.optionWithItem) {
        item.optionWithItem(item);
    }
    
    // 1.2有返回不需要传递参数的
    if (item.optionReturnStr) {
        item.optionReturnStr();
    }
    
    // 2.跳转
    if ([item isKindOfClass:[KYSettingArrowItem class]]) {
        KYSettingArrowItem *arrowItem = (KYSettingArrowItem *)item;
        
        if (arrowItem.destVcClass) {
            UIViewController *destVc = [[arrowItem.destVcClass alloc] init];
            destVc.title = arrowItem.title;
            if (arrowItem.readyForDestVc) { // 控制器的准备工作
                arrowItem.readyForDestVc(arrowItem, destVc);
            }
            [self.navigationController pushViewController:destVc animated:YES];
        }
    }
    
    // 3.check 打勾
    if ([item isKindOfClass:[KYSettingCheckItem class]]) {
        KYSettingCheckGroup *checkGroup = (KYSettingCheckGroup *)group;
        checkGroup.checkedIndex = indexPath.row;
        
        // 刷新
        [tableView reloadData];
    }
}
@end
